package com.wink_172.library.view

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.RelativeLayout
import com.wink_172.library.R
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.callback.ICallback
import com.wink_172.library.model.Constants
import com.wink_172.library.model.json.ImageData
import com.wink_172.library.utils.CommonUtils
import org.xutils.image.ImageOptions

/**
 * 选择单张图片  适用于选择头像
 */
open class SelectorSingleImage : RelativeLayout {
    open  lateinit var image: ImageView
    open  var activity: SmartActivity?=null
    open var image_data: ImageData?=null;
    open var mode=0//  0拍照和图库当前有问题  1图库
    open var callback: ICallback?=null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
    }

    override fun onFinishInflate() {
        super.onFinishInflate()
        initView()
    }

    /**
     * Activity 调用初始化操作
     */
    fun init(activity: SmartActivity,mode:Int,callback:ICallback) {
        this.activity = activity
        this.callback=callback
        this.mode=mode
    }

    private fun initView() {
        image = findViewById(R.id.image)
        image!!.setOnClickListener(OnClickListener {
            autoClick()
        })
    }

    /**
     * 强制点击
     * 主动点击
     *
     * Manifest.permission.CAMERA
     * Manifest.permission.CAMERA未添加
     * */
    fun autoClick(){
        if (!activity!!.checkPermissions(Constants.permissions)) {
            activity!!.requestPermissions(
                Constants.ACTIVITY_REQUEST_CODE_PERMISSION,
                Constants.permissions
            )
        } else {
            if(callback!=null){
                callback!!.onSendEvent(Constants.EVENT_01)
            }

            if(mode==0){
                CommonUtils.showSelPicDialog(activity!!, null)

            }else{
                CommonUtils.startActionPick(activity!!)
            }

        }
    }

    /**
     * onActivityResult  回调后主动更新
     * 获取网络头像主动更新
     */
    fun updateImage(imageData: ImageData) {
        image_data=imageData
        if (TextUtils.isEmpty(imageData.local_url) && TextUtils.isEmpty(imageData.web_url)) {
            image!!.setImageResource(R.mipmap.ic_launcher_round)
            return
        }
        if (!TextUtils.isEmpty(imageData.local_url)) {
            org.xutils.x.image().bind(image, imageData.local_url)
        } else if (!TextUtils.isEmpty(imageData.web_url)) {
            org.xutils.x.image().bind(image, imageData.web_url)
        }
    }

    /**
     * onActivityResult  回调后主动更新
     * 获取网络头像主动更新
     */
    fun updateImageCorp(imageData: ImageData) {
        image_data=imageData
        if (TextUtils.isEmpty(imageData.local_url) && TextUtils.isEmpty(imageData.web_url)) {
            image!!.setImageResource(R.mipmap.ic_launcher_round)
            return
        }

      val  options: ImageOptions = ImageOptions.Builder()
            .setUseMemCache(true)
            .setFadeIn(true)
            .setCircular(true)
            .setImageScaleType(ImageView.ScaleType.FIT_XY)
            .build()

        if (!TextUtils.isEmpty(imageData.local_url)) {
            org.xutils.x.image().bind(image, imageData.local_url,options)
        } else if (!TextUtils.isEmpty(imageData.web_url)) {
            org.xutils.x.image().bind(image, imageData.web_url,options)
        }
    }


}